package com.hk.housekeeping.service.impl;

import com.hk.housekeeping.entity.ItemsDetail;
import com.hk.housekeeping.entity.OrderItem;
import com.hk.housekeeping.entity.RespItemDetail;
import com.hk.housekeeping.mapper.ItemMapper;
import com.hk.housekeeping.service.ItemServ;
import com.hk.housekeeping.utils.PageUtils;
import com.hk.housekeeping.utils.ResponseCode;
import com.hk.housekeeping.utils.ResponseEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.List;

@Service
public class ItemServImpl implements ItemServ {
    @Autowired
    private ItemMapper itemMapper;

    @Override
    public ResponseEntity<PageUtils<List<OrderItem>>> pageSelect(int currentPage, int pageSize) {
        ResponseEntity<PageUtils<List<OrderItem>>> entity = null;
        try {
            List<OrderItem> orderItems = itemMapper.selectItems((currentPage - 1) * pageSize, pageSize);

            PageUtils<List<OrderItem>> orderItemPageUtils = new PageUtils<>();
            orderItemPageUtils.setCurrentPage(currentPage);
            orderItemPageUtils.setTotalCount(itemMapper.selectTotalCount());
            orderItemPageUtils.setData(orderItems);

            entity = ResponseEntity.success(orderItemPageUtils);
        } catch (Exception e) {
            entity = ResponseEntity.error(ResponseCode.ITEM_PAGE_SELECT_ERROR);
        }

        return entity;
    }

    @Override
    public ResponseEntity<Integer> deleteItem(int id) {
        ResponseEntity<Integer> entity = null;
        try {
            int count = itemMapper.deleteItem(id);
            if (count != 0) {
                entity = ResponseEntity.success(count);
            } else {
                entity = ResponseEntity.error(ResponseCode.ITEM_DELETE_ERROR);
            }
        } catch (Exception e) {
            entity = ResponseEntity.error(ResponseCode.ITEM_DELETE_ERROR);
        }

        return entity;
    }

    @Override
    public ResponseEntity<PageUtils<List<OrderItem>>> searchByName(String name, int currentPage, int pageSize) {
        ResponseEntity<PageUtils<List<OrderItem>>> entity = null;
        try {
            List<OrderItem> orderItems = itemMapper.selectItemByName(name, (currentPage - 1) * pageSize, pageSize);
            PageUtils<List<OrderItem>> listPageUtils = new PageUtils<>();
            listPageUtils.setCurrentPage(currentPage);
            listPageUtils.setData(orderItems);
            listPageUtils.setTotalCount(itemMapper.selectTotalCountByName(name));

            entity = ResponseEntity.success(listPageUtils);
        } catch (Exception e) {
            entity = ResponseEntity.error(ResponseCode.ITEM_SEARCH_BY_NAME_ERROR);
        }

        return entity;
    }

    @Override
    public ResponseEntity<PageUtils<List<OrderItem>>> searchByTime(String startTime, String endTime, int currentPage, int pageSize) {
        ResponseEntity<PageUtils<List<OrderItem>>> entity = null;

        try {
            List<OrderItem> orderItems = itemMapper.selectItemByTime(startTime, endTime, (currentPage - 1) * pageSize, pageSize);
            PageUtils<List<OrderItem>> listPageUtils = new PageUtils<>();
            listPageUtils.setCurrentPage(currentPage);
            listPageUtils.setData(orderItems);
            listPageUtils.setTotalCount(itemMapper.selectTotalCountByTime(startTime, endTime));

            entity = ResponseEntity.success(listPageUtils);
        } catch (Exception e) {
            entity = ResponseEntity.error(ResponseCode.ITEM_SEARCH_BY_TIME_ERROR);
        }

        return entity;
    }

    @Override
    public ResponseEntity<RespItemDetail> detailView(int itemId) {
        ResponseEntity<RespItemDetail> entity = null;
        try {
            RespItemDetail itemsDetail = itemMapper.selectItemDetail(itemId);
            entity = ResponseEntity.success(itemsDetail);
        } catch (Exception e) {
            entity = ResponseEntity.error(ResponseCode.ITEM_DETAIL_SELECT_ERROR);
        }

        return entity;
    }

    @Override
    public ResponseEntity<Integer> updateItemDetail(RespItemDetail respItemDetail) {
        ResponseEntity<Integer> entity = null;
        try {
            ItemsDetail itemsDetail = respItemDetail.getItemsDetail();

            //1、修改项目表有效时间
            int count1 = itemMapper.updateItemTime(itemsDetail.getItemId(), respItemDetail.getStartTime(), respItemDetail.getEndTime());
            //2、假删除详情表
            int count2 = itemMapper.deleteItemDetail(itemsDetail.getId());
            //3、向详情表重新插入新记录（项目表关联的id不变）
            int count3 = itemMapper.insertItemDetail(itemsDetail);

            if (count1 != 0 && count2 != 0 && count3 != 0 ) {
                entity = ResponseEntity.success(1);
            } else {
                entity = ResponseEntity.error(ResponseCode.ITEM_DETAIL_UPDATE_ERROR);
            }
        } catch (Exception e) {
            entity = ResponseEntity.error(ResponseCode.ITEM_DETAIL_UPDATE_ERROR);
        }

        return entity;
    }

    @Override
    public ResponseEntity<Integer> addItem(OrderItem orderItem) {
        ResponseEntity<Integer> entity = null;
        try {
            ItemsDetail itemsDetail = orderItem.getItemsDetail();

            //项目金额计算
            String servEndTime = itemsDetail.getServEndTime();
            String servStartTime = itemsDetail.getServStartTime();
            SimpleDateFormat sdf1 = new SimpleDateFormat("HH:mm:ss");
            long time = sdf1.parse(servEndTime).getTime() - sdf1.parse(servStartTime).getTime();
            int hour = (int)(time % 3600000 == 0 ? time/3600000 : time/3600000 + 1);
            Long hourPerPay = itemsDetail.getTimePerPay();
            Long staffPerPay = itemsDetail.getStaffPerPay();

            String endTime = orderItem.getEndTime();
            String startTime = orderItem.getStartTime();
            SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd");
            long daytime = sdf2.parse(endTime).getTime() - sdf2.parse(startTime).getTime();
            int day = (int)(daytime == 0? 1 : daytime/3600000/24);
            int count = orderItem.getCount();
            //结果
            long paidMoney = hour * hourPerPay * day + count * staffPerPay;

            OrderItem newOrderItem = new OrderItem();
            newOrderItem.setItemName(orderItem.getItemName());
            newOrderItem.setAddress(orderItem.getAddress());
            newOrderItem.setCount(count);
            newOrderItem.setStartTime(startTime);
            newOrderItem.setEndTime(endTime);
            newOrderItem.setPaidMoney(paidMoney);
            newOrderItem.setSid(orderItem.getSid());
            int count1 = itemMapper.insertItem(newOrderItem);

            ItemsDetail newItemsDetail = new ItemsDetail();
            newItemsDetail.setItemName(itemsDetail.getItemName());
            newItemsDetail.setItemImg(itemsDetail.getItemImg());
            newItemsDetail.setTimePerPay(itemsDetail.getTimePerPay());
            newItemsDetail.setStaffPerPay(itemsDetail.getStaffPerPay());
            newItemsDetail.setServStartTime(itemsDetail.getServStartTime());
            newItemsDetail.setServEndTime(itemsDetail.getServEndTime());
            newItemsDetail.setWeekendInclude(itemsDetail.getWeekendInclude());
            newItemsDetail.setDescription(itemsDetail.getDescription());
            //【关联的id】
            newItemsDetail.setItemId(newOrderItem.getId());
            int count2 = itemMapper.insertItemDetail(newItemsDetail);

            if (count1 != 0 && count2 != 0) {
                entity = ResponseEntity.success(1);
            } else {
                entity = ResponseEntity.error(ResponseCode.ITEM_ADD_ERROR);
            }
        } catch (Exception e) {
            entity = ResponseEntity.error(ResponseCode.ITEM_ADD_ERROR);
        }

        return entity;
    }
}
